<?php

namespace app\common\validate;

/**=========================================
 * Copyright © 2023-2024, WICMS.CN. All Rights Reserved
 * Project Name: WiCMS
 * Description: 管理员人验证器
 * Author: jason
 * Email: 52527998@QQ.COM
 * Date: 2024/9/24
 * Time: 下午5:17
 * File: app\common\Validate\AdminUserValidate.php
 * =========================================
 */

use think\facade\Log;
use think\Validate;

class AdminUserValidate extends Validate
{

    protected $rule = [
        'username|用户名'    => 'require|max:25',
        'nickname|昵称'      => 'require|max:25',
        'password|登录密码'  => 'require|max:25',
        'captcha|验证码'     => 'require|checkCaptcha',
        'mobile|手机号'       => 'require|max:11',
        'captchaCode|验证码' => 'require'
    ];

    protected $message = [
        'captcha.checkCaptcha' => '验证码输入错误'
    ];

    protected $scene = [
        'login'           => ['username', 'password', 'captcha', 'captchaCode'],
        'loginNotCaptcha' => ['username', 'password'],
        'create'          => ['username','nickname','password','mobile'],
        'update'          => ['username','nickname','mobile']
    ];


    /**
     * 自定义验证码验证
     *
     * @param string $value
     * @param string $rule
     * @param array  $data
     *
     * @return bool
     */
    protected function checkCaptcha(string $value, string $rule = '', array $data = []): bool
    {
        if (!isset($data['captchaCode'])) return false;
        return sha1(md5(strtolower($value) . strtoupper($value))) == $data['captchaCode'];
    }

}